package leetcode_1901_2000;

public class LeeCode_1927 {
    public static void main(String[] args) {
//        System.out.println(sumGame("5023"));
//        System.out.println(sumGame("25??"));
//        System.out.println(sumGame("?3295???"));
        System.out.println(sumGame("92??"));
    }
    private static boolean sumGame(String num) {
        int n = num.length(), sl = 0, sr = 0, cl = 0, cr = 0;
        for (int i = 0; i < n / 2; i++) {
            if (num.charAt(i) >= '0' && num.charAt(i) <= '9'){
                sl += num.charAt(i) - '0';
            }else {
                cl++;
            }
        }
        for (int i = n / 2; i < n; i++) {
            if (num.charAt(i) >= '0' && num.charAt(i) <= '9'){
                sr += num.charAt(i) - '0';
            }else {
                cr++;
            }
        }
        if (cl >= cr){
            cl -= cr;
            cr = 0;
        }else  {
            cr -= cl;
            cl = 0;
        }
        if (sl >= sr){
            sl -= sr;
            sr = 0;
        }else {
            sr -= sl;
            sl = 0;
        }
        if (sl == 0 && sr == 0 && cl == 0 && cr == 0)
            return false;
        boolean flag = sl <= 0 || cr <= 0;
        if (sr > 0 && cl > 0){
            flag = false;
        }
        if (flag){
            return true;
        }else {
            int max = Math.max(sl, sr);
            int mc = Math.max(cl, cr);
            if ((mc & 1) == 1)
                return true;
            return (mc / 2) * 9 != max;
        }
    }
}
